package cn.com.lsy.product.lepao.service.impl;

import cn.com.lsy.product.lepao.common.Constant;
import cn.com.lsy.product.lepao.model.dao.LpAdminTblMapper;
import cn.com.lsy.product.lepao.model.dao.LpMenuTblMapper;
import cn.com.lsy.product.lepao.model.dao.LpParentMenuTblMapper;
import cn.com.lsy.product.lepao.model.dao.LpRoleTblMapper;
import cn.com.lsy.product.lepao.model.entities.*;
import cn.com.lsy.product.lepao.model.mt.dao.MtAdminMapper;
import cn.com.lsy.product.lepao.model.mt.dao.MtMenuMapper;
import cn.com.lsy.product.lepao.model.mt.entities.AdminUserListDto;
import cn.com.lsy.product.lepao.model.mt.entities.AuthMenuDto;
import cn.com.lsy.product.lepao.service.IRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class RoleService implements IRoleService {

    @Autowired
    LpRoleTblMapper lpRoleTblMapper;
    @Autowired
    LpAdminTblMapper lpAdminTblMapper;
    @Autowired
    MtAdminMapper mtAdminMapper;
    @Autowired
    LpMenuTblMapper lpMenuTblMapper;
    @Autowired
    LpParentMenuTblMapper lpParentMenuTblMapper;
    @Autowired
    MtMenuMapper mtMenuMapper;

    @Override
    public List<LpRoleTbl> selectRoleList() {

        LpRoleTblExample example = new LpRoleTblExample();

        return lpRoleTblMapper.selectByExample(example);
    }

    @Override
    public boolean insertRole(LpRoleTbl param) {

        if (lpRoleTblMapper.insertSelective(param) > 0) {

            return true;

        } else {

            return false;
        }
    }

    @Override
    public boolean updateRole(LpRoleTbl param) {

        if (lpRoleTblMapper.updateByPrimaryKeySelective(param) > 0) {

            return true;

        } else {

            return false;
        }
    }

    @Override
    @Transactional
    public boolean delRole(int id) {

        //删除角色
        int delResult = lpRoleTblMapper.deleteByPrimaryKey(id);
        //该角色下的账号设置禁用
        LpAdminTblExample example = new LpAdminTblExample();
        example.createCriteria().andRoleIdEqualTo(id);

        LpAdminTbl lpAdminTbl = new LpAdminTbl();
        lpAdminTbl.setStatus(Constant.status.disable);

        if (delResult > 0) {

            lpAdminTblMapper.updateByExampleSelective(lpAdminTbl, example);
            return true;

        } else {

            throw new RuntimeException("删除失败");
        }

    }

    @Override
    public LpRoleTbl selectRoleInfo(int id) {

        return lpRoleTblMapper.selectByPrimaryKey(id);
    }

    @Override
    public LpMenuTbl selectMenuInfo(int id) {

        return lpMenuTblMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<AdminUserListDto> selectAdminListByPage(int start, int end, String searchCondition) {

        Map<String, Object> where = new HashMap<String, Object>();
        where.put("start", start);
        where.put("end", end);
        where.put("searchCondition", searchCondition);

        return mtAdminMapper.selectAdminListByPage(where);

    }

    @Override
    public int selectAdminListByCount(String searchCondition) {

        Map<String, Object> where = new HashMap<String, Object>();
        where.put("searchCondition", searchCondition);

        return mtAdminMapper.selectAdminListByCount(where);
    }

    @Override
    public boolean updateAdmin(LpAdminTbl param) {

        if (lpAdminTblMapper.updateByPrimaryKeySelective(param) > 0) {

            return true;

        } else {

            return false;
        }
    }

    @Override
    public boolean addAdmin(LpAdminTbl param) {

        if (lpAdminTblMapper.insertSelective(param) > 0) {

            return true;

        } else {

            return false;
        }
    }

    @Override
    public List<LpMenuTbl> selectMenuList() {

        LpMenuTblExample example = new LpMenuTblExample();

        return lpMenuTblMapper.selectByExample(example);
    }

    @Override
    public boolean updateMenu(LpMenuTbl param) {

        if (lpMenuTblMapper.updateByPrimaryKeySelective(param) > 0) {

            return true;

        } else {

            return false;
        }
    }

    @Override
    public boolean addMenu(LpMenuTbl param) {

        if (lpMenuTblMapper.insertSelective(param) > 0) {

            return true;

        } else {

            return false;
        }
    }

    @Override
    public boolean delMenu(int id) {

        if (lpMenuTblMapper.deleteByPrimaryKey(id) > 0) {

            return true;

        } else {

            return false;
        }
    }

    @Override
    public List<LpParentMenuTbl> selectParentMenuList() {

        LpParentMenuTblExample example = new LpParentMenuTblExample();

        return lpParentMenuTblMapper.selectByExample(example);

    }

    @Override
    public LpParentMenuTbl selectParentMenuById(int id) {

        return lpParentMenuTblMapper.selectByPrimaryKey(id);
    }

    @Override
    public boolean updateParentMenu(LpParentMenuTbl param) {

        if (lpParentMenuTblMapper.updateByPrimaryKeySelective(param) > 0) {
            return true;
        } else {
            return false;
        }
    }

    @Override
    public boolean insertParentMenu(LpParentMenuTbl param) {

        if (lpParentMenuTblMapper.insertSelective(param) > 0) {
            return true;
        } else {
            return false;
        }
    }

    @Override
    public boolean delParentMenuById(int id) {

        if (lpParentMenuTblMapper.deleteByPrimaryKey(id) > 0) {
            return true;
        } else {
            return false;
        }
    }

    @Override
    public List<AuthMenuDto> selectUserAuthMenuList(List<Integer> menuId) {

        Map<String, Object> where = new HashMap<String, Object>();
        where.put("menuId", menuId);

        return mtMenuMapper.selectUserAuthMenuList(where);
    }
}
